//
//  Box.m
//  ExceptionTest
//
//  Created by jansh on 16/4/12.
//  Copyright © 2016年 jansh. All rights reserved.
//

#import "Box.h"
#import "SomeOverException.h"
#import "SomethingException.h"

@implementation Box
-(id) init {
    self = [super init];
    
    if ( self ) {
        [self setNumber: 0];
    }
    
    return self;
}

-(void) setNumber: (NSInteger) num {
    number = num;
    
    if ( number > 10 ) {
        NSException *e = [SomeOverException
                          exceptionWithName: @"BoxOverflowException"
                          reason: @"The level is above 100"
                          userInfo: nil];
        @throw e;
    } else if ( number >= 6 ) {
        // throw warning
        NSException *e = [SomethingException
                          exceptionWithName: @"BoxWarningException"
                          reason: @"The level is above or at 60"
                          userInfo: nil];
        @throw e;
    } else if ( number < 0 ) {
        // throw exception
        NSException *e = [NSException
                          exceptionWithName: @"BoxUnderflowException"
                          reason: @"The level is below 0"
                          userInfo: nil];
        @throw e;
    }
}

-(void) pushIn {
    [self setNumber: number + 1];
}

-(void) pullOut {
    [self setNumber: number - 1];
}

-(void) printNumber {
    NSLog(@"Box number is: %ld", (long)number);
}
@end
